package com.gooapple.notepad.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.AdapterView.OnItemSelectedListener;

import com.gooapple.notepad.R;
import com.gooapple.notepad.basic.BasicActivity;
import com.gooapple.notepad.constant.Constant;
import com.gooapple.notepad.model.Picture;
import com.gooapple.notepad.model.PictureList;
import com.gooapple.notepad.util.GalleryAdapter;
import com.gooapple.notepad.util.UIManager;

public class PictureViewActivity extends BasicActivity {
	private List<Picture> pictures;
	private Gallery gallery;
	private GalleryAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initViews();
		initData();
		setListeners();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			intent.putExtra(Constant.EXTRA_PICTURE_POSITION, gallery.getSelectedItemPosition());
			setResult(RESULT_OK,intent);
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void initData() {
		Intent intent = getIntent();
		PictureList pictureList = (PictureList) intent
				.getSerializableExtra(Constant.EXTRA_PICTURE_MODEL_LABEL);
		pictures = pictureList.getPictures();
		adapter = new GalleryAdapter(pictures, this);
		gallery.setAdapter(adapter);
		int position = intent.getIntExtra(Constant.EXTRA_PICTURE_POSITION, 0);
		gallery.setSelection(position);
		UIManager.showBasicBar(this, 0, null, (position + 1) + "  0f  "
				+ pictures.size(), false, true);
	}

	@Override
	protected void initViews() {
		setContentView(R.layout.picture_view);
		gallery = (Gallery) findViewById(R.id.picture_gallery);
	}

	@Override
	protected void setListeners() {
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				UIManager.updateBasicBottomText(PictureViewActivity.this,
						(position + 1) + "  0f  " + pictures.size());
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
	}

}
